import {constantRouters} from '@/router'
import Layout from '@/views/layout/Layout'

function filterAsyncRouter(asyncRouterMap) { // 遍历后台传来的路由字符串，转换为组件对象
  try {
    const accessedRouters = asyncRouterMap.filter(route => {
      if (route.component) {
        if (route.component === 'Layout') { // Layout组件特殊处理
          route.component = Layout
        } else {
          const component = route.component
          route.component = resolve => {
            require(['@/views' + component + '.vue'], resolve)
          }
        }
      }
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children)
      }
      return true
    })
    return accessedRouters
  } catch (e) {
    console.log(e)
  }
}

const state = {
  routes: [],
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.routes = constantRouters.concat(routes)
    console.log(state.routes)
  }
}

const actions = {
  async generateRoutes({commit}, roles) {
    // 取后台路由
    return new Promise(resolve => {
      let accessedRoutes = []
      if (roles[0] === "admin") {
        accessedRoutes = [{
          path: '/qa',
          component: Layout,
          redirect: '/qa/list',
          name: '问答',
          meta: {title: '问答', icon: 'example'},
          children: [
            {
              path: 'list',
              name: '问题清单',
              component: () => import('@/views/qa/list'),
              meta: {title: '问题清单', icon: 'table'}
            },
            {
              path: 'check',
              name: '用户审核',
              component: () => import('@/views/qa/check'),
              meta: {title: '用户审核', icon: 'user'}
            },
            {
              path: 'detail/:id',
              name: '问题',
              component: () => import('@/views/qa/detail'),
              meta: {title: '问题', icon: 'table'},
              hidden: true
            },

          ]
        },]
      } else {
        accessedRoutes = [{
          path: '/qa',
          component: Layout,
          redirect: '/qa/list',
          name: '问答',
          meta: {title: '问答', icon: 'example'},
          children: [
            {
              path: 'list',
              name: '问题清单',
              component: () => import('@/views/qa/list'),
              meta: {title: '问题清单', icon: 'table'}
            },
            {
              path: 'add',
              name: '提出问题',
              component: () => import('@/views/qa/addQuestion'),
              meta: {title: '提出问题', icon: 'form'}
            },
            {
              path: 'detail/:id',
              name: '问题',
              component: () => import('@/views/qa/detail'),
              meta: {title: '问题', icon: 'table'},
              hidden: true
            },
            {
              path: 'edit/:id',
              name: '修改问题',
              component: () => import('@/views/qa/addQuestion'),
              meta: {title: '修改问题', icon: 'tree'},
              hidden: true
            }
          ]
        },]
      }
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
